package com.tamakloe.guestbook;

import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@SuppressWarnings("serial")
public class MessageUpload extends HttpServlet {
	private static final Logger log = Logger.getLogger(SignGuestbookServlet.class.getName());
    private static final String VIEW = "messageupload.jsp";
    private static final String ATTRIBUTE_FORM = "form";
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    		throws ServletException, IOException {
	    // Show view.
	    req.getRequestDispatcher(VIEW).forward(req, resp);	    

    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    		throws ServletException, IOException {
        // Prepare form bean.
    	Greeting g = new Greeting();
    	MessageUploadForm messageUploadForm = new MessageUploadForm(g);
        req.setAttribute(ATTRIBUTE_FORM, messageUploadForm);

        // Process request and get result.
        messageUploadForm.registerMessages(req);
        if (!messageUploadForm.isSuccess()){        
	        // Post back.
	        try {
				req.getRequestDispatcher(VIEW).forward(req, resp);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				log.warning(ATTRIBUTE_FORM + e.getMessage()); 
			}        
        } else {
    	    // Show view.
    	    req.getRequestDispatcher(VIEW).forward(req, resp);
        }	    
    }
}
